package com.example.admin.goodnews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.utils.SharedPreUtil;
import com.example.admin.goodnews.entity.UserInfo;

/**
 * 手机号注册页面
 * Created by admin on 2016/9/29.
 */

public class RegisteActivity extends AppCompatActivity {
    private RelativeLayout tv_cancel_registe;// 标题栏返回按钮
    private TextInputLayout til_phone_registe;// 手机号
    private EditText et_phone_registe;// 手机号文本
    private TextInputLayout til_password_registe;// 密码
    private EditText et_password_registe;// 密码文本
    private TextInputLayout til_password_registe1;// 确认密码
    private EditText et_password_registe1;// 确认密码文本
    private Button btn_confirm_registe;// 确认按钮
    private String phone;
    private String password;
    private String password1;
    private EditText edittext_phone;
    private EditText edittext_password;
    private EditText edittext_password1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registe);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        tv_cancel_registe = (RelativeLayout) findViewById(R.id.tv_cancel_registe);// 标题栏返回按钮
        til_phone_registe = (TextInputLayout) findViewById(R.id.til_phone_registe);// 手机号
        //et_phone_registe = (EditText) findViewById(R.id.et_phone_registe);// 手机号文本
        til_password_registe = (TextInputLayout) findViewById(R.id.til_password_registe);// 密码
        //et_password_registe = (EditText) findViewById(R.id.et_password_registe);// 密码文本
        til_password_registe1 = (TextInputLayout) findViewById(R.id.til_password_registe1);// 确认密码
        //et_password_registe1 = (EditText) findViewById(R.id.et_password_registe1);// 确认密码文本
        btn_confirm_registe = (Button) findViewById(R.id.btn_confirm_registe);// 确认按钮
    }

    private void initData() {
        edittext_phone = til_phone_registe.getEditText();
        til_phone_registe.setHint("请输入手机号");

        edittext_password = til_password_registe.getEditText();
        til_password_registe.setHint("请输入密码");

        edittext_password1 = til_password_registe1.getEditText();
        til_password_registe1.setHint("请确认密码");

        TextChange textchange = new TextChange();
        edittext_phone.addTextChangedListener(textchange);
        edittext_password.addTextChangedListener(textchange);
        edittext_password1.addTextChangedListener(textchange);
    }

    private void initEvent() {
        // 标题栏返回按钮
        tv_cancel_registe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 确认按钮
        btn_confirm_registe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreUtil.initSharedPreference(getApplicationContext());
                UserInfo userInfo = new UserInfo();
                userInfo.setPhone(phone);
                userInfo.setPassword(password);
                SharedPreUtil.getInstance().putUser(userInfo);

                Intent intent = new Intent();
                intent.putExtra("phone", phone);
                intent.putExtra("password", password);
                setResult(102, intent);
                finish();
            }
        });
    }

    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            phone = edittext_phone.getText().toString();
            password = edittext_password.getText().toString();
            password1 = edittext_password1.getText().toString();
            if (phone.length() == 11 && password.length() > 6 && password1.length() > 6 && password.equals(password1)) {
                btn_confirm_registe.setEnabled(true);
            } else {
                btn_confirm_registe.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            phone = edittext_phone.getText().toString();
            password = edittext_password.getText().toString();
            password1 = edittext_password1.getText().toString();
            if (!password.equals(password1)) {
                til_password_registe1.setErrorEnabled(true);
                til_password_registe1.setError("两次输入的密码不一致");
            } else {
                til_password_registe1.setErrorEnabled(false);
                btn_confirm_registe.setEnabled(true);
            }
        }
    }
}
